package com.lee.chapter01;

import com.lee.basic.anno.NotThreadSafe;

import java.util.concurrent.TimeUnit;

@NotThreadSafe
public class Test_Yield {
    private static boolean ready = false;
    private static int num = 0;
    private static int hit = 0;

    public static void main(String[] args) {
        System.out.println("hello");
        new ReadThread().start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        num = 2;
        ready = true;
    }

    private static class ReadThread extends Thread {
        @Override
        public void run() {
            while (!ready) {
                hit++;
                // 让出，出让，意思是让相同优先级的其他线程先执行
                // 让当前线程回到runnable状态（可运行状态）
                // 实际上我们无法保证真正的让出，因为当前线程可能会又一次被调度到
                Thread.yield();
            }

            System.out.println("#############hit:" + hit);
            System.out.println("#################num：" + num);
        }
    }
}
